
var root = ""
var root2 = ""
var rooturl = ""
var http = ""
if (document.location.protocol == "https:") {
    root2 = 'https://admin.hunter500.com'
    rooturl = ""  //正式环境图片
    http = "https"
}
if (document.location.protocol == "http:") {
    root = 'http://mclicn.hunter500.com'
    root2 = 'http://admin.hunter500.com'
    rooturl = "http://www.mcilcn.com"
    http = "http"
}
root = 'http://admin.yaoxinggongzuoshi.com/'

var showgonggao = true;
// MTAwMTUqNDAyODgxNTI0NTY5ZGU4NDAxNDU2OWVlN2I3ZDA0YjMqLTEqUjFGcmFtZXdvcms1LjAqMTYwMTAxMTY0MjI1MQ%3D%3D
let LTPAToken = "";
import { Toast } from "mint-ui";
// 引用axios
import axios from 'axios'
/*import checksign from './checksign.js'*/
// 自定义判断元素类型JS
function toType(obj) {
    return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
// 参数过滤函数
function filterNull(o) {
    for (var key in o) {
        if (o[key] === null) {
            delete o[key]
        }
        if (toType(o[key]) === 'string') {
            o[key] = o[key].trim()
        } else if (toType(o[key]) === 'object') {
            o[key] = filterNull(o[key])
        } else if (toType(o[key]) === 'array') {
            o[key] = filterNull(o[key])
        }
    }
    return o
}
/*
 接口处理函数
 这个函数每个项目都是不一样的，我现在调整的是适用于
 https://cnodejs.org/api/v1 的接口，如果是其他接口
 需要根据接口的参数进行调整。参考说明文档地址：
 https://cnodejs.org/topic/5378720ed6e2d16149fa16bd
 主要是，不同的接口的成功标识和失败提示是不一致的。
 另外，不同的项目的处理方法也是不一致的，这里出错就是简单的alert
 */

const CancelToken = axios.CancelToken;
var cancels = []

function apiAxios(method, url, params, success, failure) {
    // let item = params

    // console.log(params)
    // item.result = "login";
    // item.LTPAToken = "MTAwMTUqNDAyODgxNTI0NTY5ZGU4NDAxNDU2OWVlN2I3ZDA0YjMqLTEqUjFGcmFtZXdvcms1LjAqMTU5OTUyNTk2NTE3MA%3D%3D";
    // params = item;
    let source = CancelToken.source()
    // window.document.getElementById("isshow").style.display="block"
    /*  url=checksign.apendSign(url)*/
    if (params) {
        params = filterNull(params)
    }
    return new Promise(() => {
        let user
        if (JSON.parse(sessionStorage.getItem("user"))) {
            user = JSON.parse(sessionStorage.getItem("user"));
        }
        axios({
            method: method,
            url: url,
            data: method === 'POST' || method === 'PUT' || method === 'DELETE' ? params : null,
            params: method === 'GET' ? params : null,
            baseURL: root,
            withCredentials: false,
            headers: {
                Token: user ? user.TOKEN : ''
            },
        }).then(function (res) {
            success(res.data)
        }).catch(function (err) {
            console.log(err);
            success(err)
            if (err && err != "Error: Network Error" && err.message != 1) {
                Toast({
                    message: err,
                    iconClass: "icon iconfont iconxingzhuangjiehe-2"
                });
            }
        })
    })
}

function getImg(url) {
    let str = window.location.host
    return root + url;
}
// 返回在vue模板中的调用接口
export default {
    givetoken: function (token) {
        console.log(token);
        LTPAToken = token
    },
    // get: function (url, params, success, failure) {
    //     return apiAxios('GET', url + "?result=login&LTPAToken=" + LTPAToken, params, success, failure)
    // },
    get: function (url, params, success, failure) {
        return apiAxios('GET', url, params, success, failure)
    },
    post: function (url, params, success, failure) {
        return apiAxios('POST', url, params, success, failure)
    },
    put: function (url, params, success, failure) {
        return apiAxios('PUT', url, params, success, failure)
    },
    delete: function (url, params, success, failure) {
        return apiAxios('DELETE', url, params, success, failure)
    },
    getImg: function (url) {
        return getImg(url)
    },
    cancel: function () {
        cancels.forEach((item, index) => {
            delete cancels[index]
            item && item(1)
        })
    },
    showgonggao: showgonggao,
    root: root,
    rooturl: rooturl,
    http: http
}